﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace exam
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var sql = "select * from Students";
            var dt = DBHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        //查找
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Students where Name like '%{0}%'",name);
            var dt = DBHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }
        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            EditerForm editerForm = new EditerForm();
            var res =editerForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var sql = "select * from Students";
                var dt = DBHelper.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No");
            }
        }
        //更新
        private void button3_Click(object sender, EventArgs e)
        {
            var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var Name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
            var Age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
            var Score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;
            var Sex = (string)dataGridView1.SelectedRows[0].Cells["Sex"].Value;
            var Professional = (string)dataGridView1.SelectedRows[0].Cells["Professional"].Value;
            var Class = (string)dataGridView1.SelectedRows[0].Cells["Class"].Value;
            EditerForm editerForm = new EditerForm(Id, Name, Age, Score, Sex, Professional, Class);
           var res =  editerForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var sql = "select * from Students";
                var dt = DBHelper.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No");
            }

           

        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var sql = string.Format("delete from Students where Id='{0}'", Id);
                var res = DBHelper.AddOrUpdateOrDelete(sql);
                if (res == 1)
                {
                    MessageBox.Show("删除成功", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    var stusql = "select * from Students";
                    var dt = DBHelper.GetDataTable(stusql);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("当前未选择要删除的数据", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

            }
            
        }
    }
}
